package com.luo.shop.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.luo.shop.dao.GoodsBoDao;
import com.luo.shop.dao.GoodsDao;
import com.luo.shop.pojo.Goods;
import com.luo.shop.pojo.GoodsBo;
import com.luo.shop.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Collection;
import java.util.List;
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsBoDao goodsBoDao;
    @Autowired
    private GoodsDao goodsDao;

    public List<Goods> queryByCid(Long cid, Boolean createtime,Boolean price) {
        //初始化example参数
        Example example = new Example(Goods.class);
        Example.Criteria criteria = example.createCriteria();
        //添加排序  是否按新品上市的时间排序
        if (createtime!=null  && createtime){
            example.setOrderByClause("createtime desc");
        }
        //添加排序  按价格升序还是降序
        if (price!=null){
            example.setOrderByClause("price "+(price ?"desc":"asc"));
        }
        //查询cid
        if (StringUtil.isNotEmpty(cid+"")){
            criteria.andEqualTo(cid);
        }
        List<Goods> goods = goodsDao.selectByExample(example);
        for (Goods good : goods) {
            GoodsBo goodsBo = new GoodsBo();
            goodsBo.setGid(good.getId());
            good.setGoodsBos(goodsBoDao.select(goodsBo));
        }
        return goods;
    }

    public Goods queryByid(Long id) {
        Goods goods = goodsDao.selectByPrimaryKey(id);
        GoodsBo goodsBo = new GoodsBo();
        goodsBo.setGid(goods.getId());
        goods.setGoodsBos(goodsBoDao.select(goodsBo));
        return goods;
    }

}
